<?php
class item_stealrandomitem extends template_item
{
	var $default_name = "Steal Random Item";
	var $default_description = "Use this item to steal one item from people.";
	
	var $allow_multiple = 1;
	
	var $item_config = array();
	var $description = "";
	
	function load_item(&$item)
	{
		parent::load_item($item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->item['item_name'] = str_replace(array('{low}','{high}'),array($this->item_config['amount_min'],$this->item_config['amount_max']),$this->item['item_name']);
			
			$this->item_config = unserialize($item['item_config']);
		}
		else
		{
			$this->item_config['protected'] = array($this->ipsclass->vars['admin_group']);
			$this->item_config['allowed_item'] = array_keys($this->ipsclass->cache['market_items']);
			$this->item_config['most_items'] = 1;
			$this->item_config['percent'] = 50;
		}
	}
	
	function admin_form()
	{
		$groups = array();
		foreach($this->ipsclass->cache['group_cache'] as $g_id => $group)
		{
			$groups[] = array($g_id,$group['g_title']);
		}
		
		$items = array();
		foreach($this->ipsclass->cache['market_items'] as $item_id => $item)
		{
			$items[] = array($item_id,$item['item_name']);
		}
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Which groups are protected from this item?</b>",
															$this->ipsclass->adskin->form_multiselect("item[".$this->item['item_id']."][protected][]", $groups, $this->item_config['protected']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Which items can be stolen?</b>",
															$this->ipsclass->adskin->form_multiselect("item[".$this->item['item_id']."][allowed_item][]", $items, $this->item_config['allowed_item']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>How many items can be stolen at once?</b>",
															$this->ipsclass->adskin->form_input("item[".$this->item['item_id']."][most_items]", $this->item_config['most_items']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Percent chance of a successful steal</b>",
															$this->ipsclass->adskin->form_input("item[".$this->item['item_id']."][percent]", $this->item_config['percent']).'%',
													)	);
		
		return $html;
	}
	
	function admin_save(&$item_info, $input)
	{
		$save = array();
		$save['protected'] = $input['protected'] ? $input['protected'] : array();
		$save['allowed_item'] = $input['allowed_item'] ? $input['allowed_item'] : array();
		$save['percent'] = $this->ipsclass->numbers->parse_input_number($input['percent']);
		$save['most_items'] = max(intval($input['most_items']),1);
		
		$item_info['item_config'] = serialize($save);
	}
	
	function public_use_form($inventory_ids = array())
	{
		$this->ipsclass->load_language('lang_market_items');
		
		$hidden_fields = $this->_get_hidden_fields($inventory_ids);
		
		return <<<EOF
<script type="text/javascript" src='jscripts/ipb_xhr_findnames.js'></script>
<div id='ipb-get-members' style='border:1px solid #000; background:#FFF; padding:2px;position:absolute;width:210px;display:none;z-index:1'></div>
<div class='formsubtitle'>{$this->ipsclass->lang['steal_random_item']}</div>
<form action='{$this->base_url}' method='post' id='changeform'>
{$hidden_fields}
	<table class='ipbtable' cellspacing='0' cellpadding='0'>
		<tr>
			<td class='row1' align='left'>
				<b>{$this->ipsclass->lang['which_member']}</b>
			</td>
			<td class='row2' align='right'>
				<input type='input' id='member_name' name='member_name' value='' size='30' class='forminput' autocomplete='off' style='width:210px' />
			</td>
		</tr>
		<tr>
			<td class='darkrow1' align='center' colspan='2'>
				<input type='submit' name='item_used' value='{$this->ipsclass->lang['steal']}' class='forminput' />
			</td>
		</tr>
	</table>
</form>
<script type="text/javascript">
	// INIT find names
	init_js( 'changeform', 'member_name');
	// Run main loop
	setTimeout( 'main_loop()', 10 );
</script>
EOF;
	}
	
	function input_done()
	{
		return isset($this->ipsclass->input['member_name']) && isset($this->ipsclass->input['item_used']);
	}
	
	function public_use_item($inventory_ids = array()) 
	{
		$this->ipsclass->load_language('lang_market_items');
		
		$member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"members_l_display_name='".strtolower($this->ipsclass->input['member_name'])."'"));
		if(!$member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>'no_user'));
		}
		
		if(in_array($member['mgroup'],$this->item_config['protected']))
		{
			$this->ipsclass->Error(array('MSG'=>'protected_user'));
		}
		
		$inventory = array();
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_inventory','where'=>"member_id=".$member['id']));
		$this->ipsclass->DB->exec_query();
		while($inv = $this->ipsclass->DB->fetch_row())
		{
			if(in_array($inv['item'],$this->item_config['allowed_item']))
			{
				$inventory[] = $inv;
			}
		}
		$autofail = count($inventory) < $this->item_config['most_items'];
		
		$difference = $this->ipsclass->numbers->random_number(0,100,1);
		$lang = '';
		$inv_id = reset($inventory_ids);
		// Success!
		if(!$autofail && $this->ipsclass->numbers->compare($this->item_config['percent'],$difference) > 0)
		{
			srand((float) microtime() * 10000000);
			$ids = array_rand($inventory,$this->item_config['most_items']);
			if($this->item_config['most_items'] > 1)
			{
				foreach($ids as $id)
				{
					$this->ipsclass->DB->do_update('market_inventory',array(
																			'member_id' => $this->ipsclass->member['id'],
													),"i_id=".$inventory[$id]['i_id']);
				}
			}
			else
			{
				$this->ipsclass->DB->do_update('market_inventory',array(
																		'member_id' => $this->ipsclass->member['id'],
												),"i_id=".$inventory[$ids]['i_id']);
			}
			
			$this->_write_log($inv_id, "Successfully stole some random items from <a href='{$this->ipsclass->base_url}showuser={$member['id']}'>{$member['members_display_name']}</a>");
			
			$lang = 'steal_successful';
		}
		// Failed
		else
		{
			$this->_write_log($inv_id, "Failed stealing some random items from <a href='{$this->ipsclass->base_url}showuser={$member['id']}'>{$member['members_display_name']}</a>");
			
			$lang = 'steal_failed';
		}
		
		$this->delete_items($inventory_ids);
		
		$this->ipsclass->market_class->redirect($lang);
	}
}
?>